#include "bms_config.h"
#include "drv_soft_i2c.h"
#include "drv_softi2c_bq769x0.h"
#include "bms_hal_config.h"

#include "bms_analysis.h"
#include "bms_monitor.h"
#include "bms_protect.h"
#include "bms_energy.h"
#include "bms_comm.h"
#include "bms_info.h"

#include "./BSP/LED/led.h"

void BMS_SysInitialize(void)
{
	BQ769X0_InitDataTypedef InitData;
	
	// When hardware protection is triggered, the BQ chip sends an ALERT signal 
	// to the MCU, which then executes the corresponding callback function for the protection information.
	InitData.AlertOps.ocd 	 = BMS_ProtectHwOCD;
	InitData.AlertOps.scd 	 = BMS_ProtectHwSCD;
	InitData.AlertOps.ov	   = BMS_ProtectHwOV;
	InitData.AlertOps.uv 	   = BMS_ProtectHwUV;	
	InitData.AlertOps.cc 	   = BMS_MonitorHwCurrent;
	InitData.AlertOps.device = NULL;
	InitData.AlertOps.ovrd 	 = NULL;

	// BQ initialise paramter.
	InitData.ConfigData.SCDDelay	 = (BQ769X0_SCDDelayTypedef)INIT_SCD_DELAY;
	InitData.ConfigData.OCDDelay	 = (BQ769X0_OCDDelayTypedef)INIT_OCD_DELAY;
	InitData.ConfigData.UVDelay	 	 = (BQ769X0_OVDelayTypedef)INIT_UV_DELAY;
	InitData.ConfigData.OVDelay	 	 = (BQ769X0_UVDelayTypedef)INIT_OV_DELAY;
	InitData.ConfigData.UVPThreshold = INIT_UV_PROTECT * 1000;
	InitData.ConfigData.OVPThreshold = INIT_OV_PROTECT * 1000;
	
	// Initialise I2C and BQ chip.
	I2C_BusInitialize();
	BQ769X0_Initialize(&InitData);
	
	// Init BSP
	BSP_Led_Init();
		
	// Task start.
	BMS_MonitorInit();
	BMS_EnergyInit();
	BMS_ProtectInit();
	BMS_AnalysisInit();
	BMS_InfoInit();
	BMS_CommInit();
}
